package com.qtenv.datasync.worker.chuanyi;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.qtenv.datasync.api.chuanyi.ChuanYiApi;
import com.qtenv.datasync.base.ArchiveSyncer;
import com.qtenv.datasync.constant.AppConstants;
import com.qtenv.datasync.service.entity.Terminals_bf;
import com.qtenv.datasync.service.entity.chuanyi.CYTerminals;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassName CYArchiveSyncWorker
 * @Author CBL
 * @Date 2022/2/28 15:35
 **/
@Component
public class CYArchiveSyncWorker extends ArchiveSyncer {
    public CYArchiveSyncWorker() {
        super("川仪");
    }

    @Override
    public List<Terminals_bf> getTerminals() {
        String terminalData = ChuanYiApi.GetDeviceInfo();
        List<Terminals_bf> terminals = this.transform(terminalData);

        System.out.println("从厂家【" + this.getSupplier() + "】获取【档案】数量：" + terminals.size());
        return terminals;
    }

    private List<Terminals_bf> transform(String terminalData) {
        List<Terminals_bf> result = CollectionUtil.newArrayList();

        List<CYTerminals> cyTerminalList = JSON.parseArray(terminalData, CYTerminals.class);
        cyTerminalList.forEach(cyTerminals -> {
            Terminals_bf terminals = new Terminals_bf();
            BeanUtil.copyProperties(cyTerminals, terminals);
            terminals.setTenant_id(AppConstants.APP_TENANT_ID);
            terminals.setSupplierID(AppConstants.CY_SUPPLIER_ID);
            terminals.setTerminalMark(1);
            terminals.setUserArea(AppConstants.APP_TENANT_ID);
            if (StrUtil.isEmpty(terminals.getTerminalID())) {
                terminals.setTerminalID(cyTerminals.getSIM());
            }
            if (terminals.getTerminalInstallDate() == null && cyTerminals.getPutIntoUseDate() != null) {
                terminals.setTerminalInstallDate(cyTerminals.getPutIntoUseDate());
            }

            result.add(terminals);
        });

        return result;
    }

    public static void main(String[] args) {
        CYArchiveSyncWorker worker = new CYArchiveSyncWorker();
//        System.out.println(JSON.toJSONString(worker.getTerminals(), true));
        worker.syncTerminals();
    }
}
